import configBase from '../config/index'
import {
    Plugin as TimelinePointer
} from 'gantt-schedule-timeline-calendar/dist/plugins/timeline-pointer.esm.min.js';
import {
    Plugin as Selection
} from "gantt-schedule-timeline-calendar/dist/plugins/selection.esm.min.js"
import {
    Plugin as ItemResizing
} from "gantt-schedule-timeline-calendar/dist/plugins/item-resizing.esm.min.js";
import {
    Plugin as ItemMovement
} from 'gantt-schedule-timeline-calendar/dist/plugins/item-movement.esm.min.js'

import moment from 'moment'

function addListenClick(element, data) {
    const onClick = (e) => {
        e.preventDefault()
        // console.log(data)
        return false
    }
    element.addEventListener('contextmenu', onClick);
    return {
        update(element, newData) {
            data = newData;
        },
        destroy(element, data) {
            element.removeEventListener('click', onClick);
        }
    };
}
export default {
    setConfig(columns, rows, items, chartParam) {
        const config = {
            licenseKey: configBase.license.key,
            list: {
                columns,
                rows,
            },
            // 表头日期
            chart: {
                items: items,
                time: {
                    from: moment(chartParam.from),
                    to: moment(chartParam.to),
                },
            },
            actions: {
                'chart-timeline-items-row-item': [addListenClick] // 监听右击事件
            },
            // plugins: [
            //     TimelinePointer({}), // timeline pointer must go first before selection, resizing and movement
            //     Selection(),
            //     ItemResizing(), // resizing must go before movement
            //     ItemMovement(movementPluginConfig),
            //   ],
            plugins: [
                TimelinePointer({}), // timeline pointer must go first before selection, resizing and movement

                Selection({
                    items: false,
                    rows: true,
                    grid: true,
                    rectStyle: {
                        opacity: '0.0'
                    },
                    canSelect(type, currentlySelecting) {
                        if (type === 'chart-timeline-grid-row-block') {
                            return currentlySelecting.filter(selected => {
                                if (!selected.row.canSelect) return false;
                                for (const item of selected.row._internal.items) {
                                    if (
                                        (item.time.start >= selected.time.leftGlobal && item.time.start <= selected.time.rightGlobal) ||
                                        (item.time.end >= selected.time.leftGlobal && item.time.end <= selected.time.rightGlobal) ||
                                        (item.time.start <= selected.time.leftGlobal && item.time.end >= selected.time.rightGlobal)
                                    ) {
                                        return false;
                                    }
                                }
                                return true;
                            });
                        }
                        return currentlySelecting;
                    },
                    canDeselect(type, currently, all) {
                        if (type === 'chart-timeline-grid-row-blocks') {
                            return all.selecting['chart-timeline-grid-row-blocks'].length ? [] : currently;
                        }
                        return [];
                    }
                }),
                ItemResizing(), // resizing must go before movement
                // 拖动 x 横向， y 纵向
                ItemMovement({
                    moveable: 'x',
                    resizerContent: '<div class="resizer">-></div>',
                    ghostNode: false,
                    collisionDetection: false,
                    snapStart(time, diff, item) {
                        if (Math.abs(diff) > 14400000) {
                            return time + diff
                        }
                        return time
                    },
                    snapEnd(time, diff, item) {
                        if (Math.abs(diff) > 14400000) {
                            return time + diff
                        }
                        return time
                    }
                }),
            ],
            // 配置中文
            locale: {
                name: "zh",
                weekdays: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
                months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            },
        };
        return config
    }
}